<template>
  <div class="login-container">
    <div class="login-background">
      <div class="shape shape-1"></div>
      <div class="shape shape-2"></div>
      <div class="shape shape-3"></div>
    </div>
    <div class="login-content">
      <div class="left-section">
        <div class="welcome-content">
          <h1>AI 学途引航者</h1>
          <h2>管理端登录</h2>
          <div class="store-image">
            <img src="../assets/static/login/logo.png" style="border-radius: 50%;" alt="store">
          </div>
        </div>
      </div>
      <div class="right-section">
        <div class="login-box">
          <h2 class="login-title">登录</h2>
          <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" class="login-form">
            <el-form-item prop="username" class="slide-up">
              <el-input v-model="loginForm.username" placeholder="用户名">
                <template #prefix>
                  <el-icon class="input-icon">
                    <User />
                  </el-icon>
                </template>
              </el-input>
            </el-form-item>
            <el-form-item prop="password" class="slide-up">
              <el-input v-model="loginForm.password" type="password" placeholder="密码" show-password>
                <template #prefix>
                  <el-icon class="input-icon">
                    <Lock />
                  </el-icon>
                </template>
              </el-input>
            </el-form-item>
            
            <el-form-item prop="code" class="slide-up verification-code-container">
              <div class="code-input-group">
                <el-input 
                  v-model="loginForm.code" 
                  placeholder="验证码" 
                  maxlength="4"
                  class="code-input"
                >
                  <template #prefix>
                    <el-icon class="input-icon">
                      <Key />
                    </el-icon>
                  </template>
                </el-input>
                <div class="code-image" @click="handleCodeClick" :class="{ 'disabled': isCodeDisabled }">
                  <img :src="codeUrl" alt="验证码" v-if="codeUrl">
                  <span v-if="isCodeDisabled" class="countdown">{{ countdown }}s</span>
                </div>
              </div>
            </el-form-item>
            
            <el-form-item class="slide-up">
              <el-button type="primary" :loading="loading" class="login-button" @click="handleLogin">
                登录
              </el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, onUnmounted } from 'vue'
import { User, Lock, Key } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import service from '@/request/request'
import { useUserStore } from '@/store/user'



const userStore = useUserStore()
const router = useRouter()
const loginFormRef = ref(null)
const loading = ref(false)
const codeUrl = ref();
const isCodeDisabled = ref(false)
const countdown = ref(0)
let timer = null

const loginForm = ref({
  username: '',
  password: '',
  code:'',
  uuid:''
})

const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度应在3-20个字符之间', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度应在6-20个字符之间', trigger: 'blur' }
  ],
  code: [
    { required: true, message: '请输入验证码', trigger: 'blur' },
    { len: 4, message: '验证码长度应为4位', trigger: 'blur' }
  ]
}

const getCode = () => {
  service({
    url: "/user/getCode",
      method: "GET",
  }).then((res) => {
    codeUrl.value = "data:image/gif;base64," + res.data.data.img,
    loginForm.value.uuid = res.data.data.uuid
  })
}

const handleCodeClick = () => {
  if (isCodeDisabled.value) return
  getCode()
  startCountdown()
}

const startCountdown = () => {
  isCodeDisabled.value = true
  countdown.value = 5
  timer = setInterval(() => {
    countdown.value--
    if (countdown.value <= 0) {
      clearInterval(timer)
      isCodeDisabled.value = false
    }
  }, 1000)
}

const handleLogin = async () => {
  if (!loginFormRef.value) return

  try {
    loading.value = true
    await loginFormRef.value.validate()

    // 发送登录请求
    const res = await service({
      url: "/user/login",
      method: "POST",
      data: {
        username: loginForm.value.username,
        password: loginForm.value.password,
        code: loginForm.value.code,
        uuid: loginForm.value.uuid
      }
    })

    if (res.data.code === 200) {
      // console.log(res.data.data.authenticationToken);
      // console.log(res.data.data.refurbishToken); 
      //用户信息
      // console.log(res.data.data.userInfo);
      
      // 保存用户信息到Pinia
      userStore.setUserInfo(JSON.parse(res.data.data.userInfo))

      //认证token
      localStorage.setItem("authenticationToken",res.data.data.authenticationToken)
      //刷新token
      localStorage.setItem("refurbishToken",res.data.data.refurbishToken)
      
      ElMessage.success('登录成功')

      router.push('/index')
    } else {
      ElMessage.error(res.data.message || '登录失败')
      // 登录失败时刷新验证码
      getCode()
    }
  } catch (error) {
    console.error('登录失败:', error)
    ElMessage.error('网络错误,请重试!')

    // 登录失败时刷新验证码
    // getCode()
  } finally {
    loading.value = false
  }
}

onMounted(()=>{
  getCode();
})

onUnmounted(() => {
  if (timer) {
    clearInterval(timer)
  }
})
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(to right, #3ffcd3, #4A90E2);
  position: relative;
  overflow: hidden;
}

.login-background {
  position: absolute;
  width: 100%;
  height: 100%;
  z-index: 1;
}

.shape {
  position: absolute;
  border-radius: 50%;
  filter: blur(50px);
}

.shape-1 {
  width: 500px;
  height: 500px;
  background: linear-gradient(135deg, #00c6fb20 0%, #005bea10 100%);
  top: -250px;
  left: -100px;
  animation: floatAnimation 8s ease-in-out infinite;
}

.shape-2 {
  width: 400px;
  height: 400px;
  background: linear-gradient(135deg, #00c6fb15 0%, #005bea08 100%);
  bottom: -200px;
  right: -100px;
  animation: floatAnimation 12s ease-in-out infinite;
}

.shape-3 {
  width: 300px;
  height: 300px;
  background: linear-gradient(135deg, #00c6fb10 0%, #005bea05 100%);
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  animation: floatAnimation 10s ease-in-out infinite;
}

@keyframes floatAnimation {

  0%,
  100% {
    transform: translate(0, 0) rotate(0deg);
  }

  25% {
    transform: translate(50px, -30px) rotate(10deg);
  }

  50% {
    transform: translate(0, 50px) rotate(-5deg);
  }

  75% {
    transform: translate(-50px, -25px) rotate(5deg);
  }
}

.login-content {
  display: flex;
  width: 1000px;
  height: 600px;
  background: rgba(255, 255, 255, 0.9);
  border-radius: 20px;
  box-shadow: 0 15px 50px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  position: relative;
  z-index: 2;
}

.left-section {
  flex: 1;
  background-color: #4AB1E2;
  padding: 40px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  color: white;
  position: relative;
}

.welcome-content {
  text-align: center;
}

.welcome-content h1 {
  font-size: 48px;
  font-weight: 700;
  margin-bottom: 20px;
  letter-spacing: 2px;
}

.welcome-content h2 {
  font-size: 24px;
  margin-bottom: 40px;
  letter-spacing: 1px;
}

.store-image {
  /* 设置为圆形 */

  width: 200px;
  height: 200px;
  margin: 0 auto;
  animation: float 3s ease-in-out infinite;
}

.store-image img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.right-section {
  flex: 1;
  padding: 40px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.login-box {
  width: 100%;
  max-width: 400px;
  margin: 0 auto;
  animation: slideIn 0.6s cubic-bezier(0.4, 0, 0.2, 1);
}

.login-title {
  font-size: 32px;
  color: #2c3e50;
  margin-bottom: 40px;
  text-align: center;
  font-weight: 600;
}

.login-form {
  margin-top: 20px;
}

.slide-up {
  animation: slideUp 0.6s cubic-bezier(0.4, 0, 0.2, 1);
  animation-fill-mode: both;
}

.slide-up:nth-child(1) {
  animation-delay: 0.2s;
}

.slide-up:nth-child(2) {
  animation-delay: 0.4s;
}

.slide-up:nth-child(3) {
  animation-delay: 0.6s;
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

:deep(.el-input__wrapper) {
  background: #f8fafc !important;
  box-shadow: none !important;
  border: 2px solid transparent !important;
  border-radius: 12px !important;
  transition: all 0.3s ease;
}

:deep(.el-input__wrapper:hover) {
  background: #f1f5f9 !important;
}

:deep(.el-input__wrapper.is-focus) {
  border-color: #00c6fb !important;
  background: #ffffff !important;
  box-shadow: 0 0 0 4px rgba(0, 198, 251, 0.1) !important;
}

.input-icon {
  font-size: 18px;
  color: #64748b;
  transition: color 0.3s ease;
}

.login-button {
  width: 100%;
  height: 45px;
  font-size: 16px;
  font-weight: 600;
  letter-spacing: 1px;
  background: linear-gradient(135deg, #00c6fb 0%, #005bea 100%);
  border: none;
  border-radius: 12px;
  transition: all 0.3s ease;
}

.login-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 10px 20px rgba(0, 198, 251, 0.3);
}

@keyframes float {

  0%,
  100% {
    transform: translateY(0);
  }

  50% {
    transform: translateY(-20px);
  }
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateX(30px);
  }

  to {
    opacity: 1;
    transform: translateX(0);
  }
}

@media (max-width: 768px) {
  .login-content {
    flex-direction: column;
    width: 90%;
    height: auto;
  }

  .left-section {
    padding: 30px;
  }

  .right-section {
    padding: 30px;
  }

  .welcome-content h1 {
    font-size: 36px;
  }

  .welcome-content h2 {
    font-size: 20px;
  }

  .store-image {
    width: 150px;
    height: 150px;
  }
}

/* 删除极验相关样式 */
#captcha,
.geetest_holder.geetest_wind,
.geetest_btn_click {
  display: none;
}

.verification-code-container {
  margin-bottom: 20px;
}

.code-input-group {
  display: flex;
  align-items: center;
  gap: 12px;
}

.code-input {
  flex: 1;
  max-width: calc(100% - 140px);
}

.code-image {
  width: 130px;
  height: 44px;
  border-radius: 12px;
  overflow: hidden;
  cursor: pointer;
  transition: all 0.3s ease;
  background: #f8fafc;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

.code-image.disabled {
  cursor: not-allowed;
  opacity: 0.7;
  transform: none !important;
  box-shadow: none !important;
}

.code-image img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  padding: 2px;
}

.countdown {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  font-weight: bold;
}
</style>